package com.cgc.venus.server.core.utils;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * Created by 业哥 on 16-5-27.
 */
public class StringUtil {
    /**
     * 字符串是否为空或者null
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if(str==null){
            return true;
        }
        if("".equals(str)){
            return true;
        }
        return false;
    }

    /**
     * 字符串是否含有下划线
     * @param str
     * @return
     */
    public static boolean stringHasUnderline(String str){
        if(isEmpty(str)){
            return false;
        }
        if(str.indexOf("_")!=-1){
            return true;
        }
        return false;
    }


    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        return uuid;
    }

    /**
     * byte(字节)根据长度转成kb(千字节)
     * 和mb(兆字节)
     *
     * @param bytes
     * @return
     */
    public static float bytes2kbFloat(long bytes) {
        BigDecimal filesize = new BigDecimal(bytes);
//        BigDecimal megabyte = new BigDecimal(1024 * 1024);
//        float returnValue = filesize.divide(megabyte, 1, BigDecimal.ROUND_UP)
//                .floatValue();
//        if (returnValue > 1)
//            return (returnValue + "MB");
        BigDecimal kilobyte = new BigDecimal(1024);
        float returnValue = filesize.divide(kilobyte, 1, BigDecimal.ROUND_UP)
                .floatValue();
        return returnValue;
    }

    /**
     * byte(字节)根据长度转成kb(千字节)
     * 和mb(兆字节)
     *
     * @param bytes
     * @return
     */
    public static Double bytes2kbDouble(long bytes) {
        BigDecimal filesize = new BigDecimal(bytes);
//        BigDecimal megabyte = new BigDecimal(1024 * 1024);
//        float returnValue = filesize.divide(megabyte, 1, BigDecimal.ROUND_UP)
//                .floatValue();
//        if (returnValue > 1)
//            return (returnValue + "MB");
        BigDecimal kilobyte = new BigDecimal(1024);
        Double returnValue = filesize.divide(kilobyte, 1, BigDecimal.ROUND_UP).doubleValue();
        return returnValue;
    }

    /*
    * 将对象转为json
    * */
    public static String ObjectToJson(Object object){
        ObjectMapper mapper = new ObjectMapper();
        String json="";
        try {
            json =  mapper.writeValueAsString(object);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    //获取指定位数的随机字符串(包含小写字母、大写字母、数字,0<length)
    public static String getRandomString(int length) {
        //随机字符串的随机字符库
        String KeyString = "abcdefghijklmnopqrstuvwxyz0123456789";
        StringBuffer sb = new StringBuffer();
        int len = KeyString.length();
        for (int i = 0; i < length; i++) {
            sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));
        }
        return sb.toString();
    }
 }
